package com.hanserwei.community.controller;

import com.hanserwei.community.entity.common.ApiResponse;
import com.hanserwei.community.entity.po.RepairType;
import com.hanserwei.community.entity.request.repair.RepairTypeSaveRequest;
import com.hanserwei.community.enums.ResponseCodeEnum;
import com.hanserwei.community.service.RepairTypeService;
import com.hanserwei.community.uitils.ThrowUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/repair/type")
@Tag(name = "RepairTypeController", description = "报修类型管理接口")
@RequiredArgsConstructor
public class RepairTypeController {

    private final RepairTypeService repairTypeService;

    @Operation(summary = "查询启用的报修类型")
    @GetMapping("/list")
    public ApiResponse<List<RepairType>> list() {
        List<RepairType> list = repairTypeService.listActive();
        return ApiResponse.success("查询成功！", list);
    }

    @Operation(summary = "新增报修类型")
    @PostMapping("/add")
    public ApiResponse<Boolean> add(@RequestBody RepairTypeSaveRequest request) {
        repairTypeService.createType(request);
        return ApiResponse.success("新增成功！", true);
    }

    @Operation(summary = "更新报修类型")
    @PostMapping("/update/{id}")
    public ApiResponse<Boolean> update(@PathVariable Integer id, @RequestBody RepairTypeSaveRequest request) {
        repairTypeService.updateType(id, request);
        return ApiResponse.success("更新成功！", true);
    }

    @Operation(summary = "删除报修类型")
    @DeleteMapping("/delete")
    public ApiResponse<Boolean> delete(@RequestParam("id") Integer id) {
        ThrowUtils.throwIf(id == null || id <= 0, ResponseCodeEnum.INVALID_PARAMETER, "类型ID参数错误！");
        boolean result = repairTypeService.removeById(id);
        ThrowUtils.throwIf(!result, ResponseCodeEnum.SYSTEM_ERROR, "删除失败！");
        return ApiResponse.success("删除成功！", true);
    }
}
